//
//  ManuView.m
//  Unity-iPhone
//
//  Created by Karl on 2017/3/28.
//
//

#import "ManuView.h"
#import "ManuItem.h"
#import <QuartzCore/QuartzCore.h>
#import "UIView+PopAnimation.h"

@interface ManuView ()<CAAnimationDelegate,ManuItemDelegate>

@property (nonatomic,strong) UIButton *manuBtn;
@property (nonatomic,strong) UIView *manuBg;
@property (nonatomic,strong) NSArray *items;
@property (nonatomic,strong) NSMutableArray *itemViews;
@end

@implementation ManuView{
    BOOL _pop;
    CGFloat _itemsHeight;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        
        CGFloat scale = 1.0;
        if (isPad) {
            scale = SizeScaleX;
        }else{
            scale = SizeScaleY;
        }
        
        _pop = NO;
    
        _items = @[@{@"title":Babel(@"manu_home"),@"image":@"manu_home",@"url":Babel(@"home_url"),@"in_app":@(YES)},
                   @{@"title":Babel(@"manu_donate"),@"image":@"manu_donate",@"url":Babel(@"donate_url"),@"in_app":@(NO)},
                   @{@"title":Babel(@"manu_tutorial"),@"image":@"manu_teach",@"url":@"",@"in_app":@(YES)},
                   @{@"title":Babel(@"manu_tlp"),@"image":@"manu_prince",@"url":Babel(@"tlp_url"),@"in_app":@(YES)}];
        
        _itemViews = [NSMutableArray array];
    
        _manuBg = [UIView new];
        [_manuBg setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.6]];
        [_manuBg.layer setCornerRadius:22.0 * scale];
        [_manuBg.layer setMasksToBounds:YES];
        [self addSubview:_manuBg];
        
        for (NSInteger i = _items.count - 1; i > -1; i--) {
//            ManuItem *item = [[ManuItem alloc] initWithTitle:[_items[i] objectForKey:@"title"] image:[_items[i] objectForKey:@"image"] url:[_items[i] objectForKey:@"url"] size:CGSizeMake(35.3 * scale, 48.3 * scale)];
            ManuItem *item = [[ManuItem alloc] initWithDic:(NSDictionary*)_items[i] size:CGSizeMake(35.3*scale, 48.3*scale)];
            item.delegate = self;
//            [item setFrame:CGRectMake(self.width/2, self.width+48.3*i, 35.3, 48.3)];
            [self addSubview:item];
            [item setHidden:YES];
            [_itemViews insertObject:item atIndex:0];
            _itemsHeight += item.itemHeight;
        }
        
        _manuBtn = [UIButton new];
        [_manuBtn setBackgroundImage:[UIImage imageNamed:@"all_manu"] forState:UIControlStateNormal];
        [_manuBtn addTarget:self action:@selector(manuBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_manuBtn];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat scale = 1.0;
    if (isPad) {
        scale = SizeScaleX;
    }else{
        scale = SizeScaleY;
    }
    
    _manuBtn.left = 0;
    _manuBtn.top = 0;
    _manuBtn.width = self.width;
    _manuBtn.height = self.width;
    
//    for (int i = 0; i<_itemViews.count; i++) {
//        UIView *view = (UIView *)_itemViews[i];
//        view.top = _manuBtn.bottom +3+ 48.3*i;
//        view.centerX = _manuBtn.centerX;
//        view.width = 35.3;
//        view.height = 48.3;
//
//    }
    
    for (ManuItem*view in _itemViews) {
        view.width = 35.3 * scale;
        view.height = view.itemHeight;//48.3 * scale;
    }
    
    _manuBg.top = 0;
    _manuBg.centerX = _manuBtn.centerX;
    _manuBg.width = 44 * scale;
}

- (void)manuBtnAction:(id)sender{
    
    CGFloat scale = 1.0;
    if (isPad) {
        scale = SizeScaleX;
    }else{
        scale = SizeScaleY;
    }
    
    [UIView beginAnimations:@"bgPop" context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    if (_pop) {
        _manuBg.height = 50 * scale ;

    }else{
        _manuBg.height = 88 * scale + _itemsHeight + 10;//48.3  * scale * _itemViews.count;

    }

    [UIView commitAnimations];
    
    CGFloat h = 0;
    
    for (int i = 0; i<_itemViews.count; i++) {
        ManuItem *view = (ManuItem *)_itemViews[i];
        view.hidden = NO;
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
        if (i == _itemViews.count - 1) {
            animation.delegate = self;
        }
        animation.duration = 0.3;
        animation.repeatCount = 1;
        animation.beginTime = CACurrentMediaTime();
        animation.autoreverses = NO;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        
        if (_pop) {
            animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(view.centerX, view.centerY)];
            animation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.width/2, self.width/2)];
            
        }else{
            
            h+=view.itemHeight;
            
            animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.width/2, self.width/2)];
            animation.toValue = [NSValue valueWithCGPoint:CGPointMake(_manuBtn.centerX, _manuBtn.bottom +3 * scale * ( i + 1 ) + h - view.itemHeight/2)];
        }
        
        animation.removedOnCompletion = NO;
        animation.fillMode = kCAFillModeForwards;
        [view.layer addAnimation:animation forKey:@"move"];
        
    }
    _pop = !_pop;
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    CGFloat scale = 1.0;
    if (isPad) {
        scale = SizeScaleX;
    }else{
        scale = SizeScaleY;
    }
    CGFloat h = 0;

    for (int i = 0; i<_itemViews.count; i++) {
        ManuItem *view = (ManuItem *)_itemViews[i];
        h+=view.itemHeight;

        if (_pop) {
            view.center = CGPointMake(_manuBtn.centerX, _manuBtn.bottom +3 * scale *(i+1)+ h - view.itemHeight/2);
            UIView *view = (UIView *)_itemViews[1];
            [view buttonPop];
        }else{
            view.center = _manuBtn.center;
        }
        
    }
    
}

- (void)manuItemAction:(NSString *)action{
    if ([_delegate respondsToSelector:@selector(manuViewAction:)]) {
        [_delegate manuViewAction:action];
    }
}

@end
